<?php
namespace Home\Model;
Use Think\Model;
class DianzanModel extends Model{
    //增加赞
    //1代表没有赞过 增加赞
    //2代表已经赞过 返回点赞人信息
    public function jiazan($data){
        foreach($data as $v){
            if(!$v){
                return false;   
            }            
        }
        $zanList = $this->haszan($data);
        $data['userinfo'] = D('Personal')->hasPersonal($data['uid']);
        if(!$zanList){
            $zanInfo = $this->add($data);
            $where['id']=$data['mid'];
            $mood = M("mood")->where($where)->setInc('hits',1);
            $data['status']=1;
            return $data;
        }else {
            $data['status']=2;
            //如果赞过了。就取消这个人对着个动态的的赞
            return $data;
        }
        
    }
    //查看是否赞过
    public function haszan($data){
        $where['mid']=$data['mid'];
        $zanList = $this->where($where)->select();
        foreach($zanList as $k=>$v){
            if($v['uid'] == $data['uid']){
                $zanInfo = $this->delzan($data);
                if($zanInfo){
                    return true;
                }
            }else{
                return false;
            }
        }
    }
    
    //查看一个动态有多少的赞
    public function contzan($mid){
    	if($mid){
    		$where['d.mid']=$mid;
    		$join = "left join __PERSONAL__ p on d.uid=p.uid";
    		$field = "d.*,p.user_tx";
    		$zanList = $this->alias('d')
    						->join($join)
    						->field($field)
    						->where($where)
    						->select();
    		if($zanList){
    			return $zanList;
    		}
    	}
    }
    
    
    public function delzan($data){
        if(!empty($data)){
            $where['uid']=$data['uid'];
            $where['mid']=$data['mid'];
            $zanInfo = $this->where($where)->delete();
            if($zanInfo){
                return true;
            }else{
                return false;
            }
        }
    }
}